package com.weiyi.mashangzhu.Landlord.Adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.util.Util;
import com.squareup.okhttp.Request;
import com.weiyi.mashangzhu.API.API;
import com.weiyi.mashangzhu.Application.MyApp;
import com.weiyi.mashangzhu.Landlord.Activity.EditHouseActivity;
import com.weiyi.mashangzhu.Landlord.Bean.HouseBean_L;
import com.weiyi.mashangzhu.R;
import com.weiyi.mashangzhu.Roomer.Activity.AboutActivity;
import com.weiyi.mashangzhu.Roomer.Activity.AllCommentActivity;
import com.weiyi.mashangzhu.Roomer.Bean.CouponBean;
import com.weiyi.mashangzhu.Utils.GlideImgManager;
import com.weiyi.mashangzhu.Utils.LoadingDialog;
import com.weiyi.mashangzhu.Utils.SPUtils;
import com.weiyi.mashangzhu.Utils.T;
import com.weiyi.mashangzhu.Utils.UiUtil;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

public class HouseAdapter extends BaseAdapter {
    List<HouseBean_L.DataBean.OnsalehouselistBean> list_o;
    List<HouseBean_L.DataBean.PutonsalehouselistBean> list_p;
    Context context;
    String info;

    public HouseAdapter(List<HouseBean_L.DataBean.OnsalehouselistBean> list_o, List<HouseBean_L.DataBean.PutonsalehouselistBean> list_p, Context context, String info) {
        this.list_o = list_o;
        this.list_p = list_p;
        this.context = context;
        this.info = info;
    }

    @Override
    public int getCount() {
        if (info.equals("o")) {
            return list_o.size();
        } else {
            return list_p.size();
        }

    }

    @Override
    public Object getItem(int position) {
        if (info.equals("o")) {
            return list_o.get(position);
        } else {
            return list_p.get(position);
        }

    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_house_l, null);
            viewHolder.img = (ImageView) convertView.findViewById(R.id.img);
            viewHolder.title = (TextView) convertView.findViewById(R.id.order_title);
            viewHolder.type = (TextView) convertView.findViewById(R.id.order_type);
            viewHolder.flag = (TextView) convertView.findViewById(R.id.order_flag);
            viewHolder.price = (TextView) convertView.findViewById(R.id.order_price);
            viewHolder.edit = (TextView) convertView.findViewById(R.id.edit);
            viewHolder.comment = (TextView) convertView.findViewById(R.id.comment);
            viewHolder.down = (TextView) convertView.findViewById(R.id.down);
            convertView.setTag(viewHolder);

        }
        viewHolder = (ViewHolder) convertView.getTag();
        if (info.equals("o")) {
            if (list_o!=null&&list_o.size()!=0){
                GlideImgManager.glideLoader(context, API.BaseApi_url + list_o.get(position).getHImg1(), R.mipmap.errer, viewHolder.img);
                viewHolder.title.setText(list_o.get(position).getHTitle() + "");
                viewHolder.type.setText(list_o.get(position).getHRoom() + "居室");
                viewHolder.flag.setText("上线中");
                viewHolder.price.setText("¥" + list_o.get(position).getHPrice() + "/晚");
                viewHolder.down.setText("房屋下线");
                viewHolder.down.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        LoadingDialog.show(context);
                        OkHttpUtils.get().
                                addParams("token", SPUtils.get(context, "token", "") + "")
                                .addParams("id", list_o.get(position).getId() + "")
                                .url(API.BaseApi_L + API.PutOnSale).build().execute(new StringCallback() {
                            @Override
                            public void onError(Request request, Exception e) {
                                T.showShort(context, "网络连接失败，请检查一下网络设置");
                                LoadingDialog.dismiss(context);
                            }

                            @Override
                            public void onResponse(String response) {
                                LoadingDialog.dismiss(context);
                                Log.e("dddddddd", "===" + response);
                                try {
                                    JSONObject jsonObject = new JSONObject(response);
                                    if (jsonObject.getInt("status") == 0) {
                                        list_o.remove(position);
                                        notifyDataSetChanged();
                                    }

                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }

                            }
                        });

                    }
                });
                viewHolder.comment.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(context, AllCommentActivity.class);
                        intent.putExtra("id", list_o.get(position).getId() + "");
                        intent.putExtra("type", "house_l");
                        context.startActivity(intent);
                    }
                });
                viewHolder.edit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        MyApp.img_url.clear();
                        Intent intent=new Intent(context, EditHouseActivity.class);
                        intent.putExtra("id",list_o.get(position).getId()+"");
                        context.startActivity(intent);
                    }
                });
            }

        } else {
            if (list_p!=null&&list_p.size()!=0){
                GlideImgManager.glideLoader(context, API.BaseApi_url + list_p.get(position).getHImg1(), R.mipmap.errer, viewHolder.img);
                viewHolder.title.setText(list_p.get(position).getHTitle() + "");
                viewHolder.type.setText(list_p.get(position).getHRoom() + "居室");
                viewHolder.flag.setText("已下线");
                viewHolder.price.setText("¥" + list_p.get(position).getHPrice() + "/晚");
                viewHolder.down.setText("房屋上线");
                viewHolder.down.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        LoadingDialog.show(context);
                        OkHttpUtils.get().
                                addParams("token", SPUtils.get(context, "token", "") + "")
                                .addParams("id", list_p.get(position).getId() + "")
                                .url(API.BaseApi_L + API.PutOnSale).build().execute(new StringCallback() {
                            @Override
                            public void onError(Request request, Exception e) {
                                T.showShort(context, "网络连接失败，请检查一下网络设置");
                                LoadingDialog.dismiss(context);
                            }

                            @Override
                            public void onResponse(String response) {
                                LoadingDialog.dismiss(context);
                                try {
                                    JSONObject jsonObject = new JSONObject(response);
                                    if (jsonObject.getInt("status") == 0) {
                                        list_p.remove(position);
                                        notifyDataSetChanged();
                                    }

                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }

                            }
                        });

                    }
                });
                viewHolder.comment.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(context, AllCommentActivity.class);
                        intent.putExtra("id", list_p.get(position).getId() + "");
                        intent.putExtra("type", "house_l");
                        context.startActivity(intent);
                    }
                });
                viewHolder.edit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        MyApp.img_url.clear();
                        Intent intent=new Intent(context, EditHouseActivity.class);
                        intent.putExtra("id",list_p.get(position).getId()+"");
                        context.startActivity(intent);
                    }
                });
            }

        }
        return convertView;
    }

    class ViewHolder {
        ImageView img;
        TextView title, type, flag, price, edit, comment, down;
    }
}
